package com.icangya.DAO.impl;


import com.icangya.DAO.IExpendRecordDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.Utils.SumPageNum;
import com.icangya.domain.ExpendRecord;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.icangya.Value.FootBall.FOOT_PAGE_ROW;
import static com.icangya.Value.StaticValue.PHONE_PAGE_ROW;

@Repository
public class ExpendRecordDAOImpl extends BaseDAO<ExpendRecord> implements IExpendRecordDAO{


    @Override
    public List getExpendRecordByOpenId(String openId) {
        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql=" FROM ExpendRecord er where er.openId=? order by er.expendTime desc ";
        Query query=session.createQuery(hql);
        query.setParameter(0,openId);
        query.setMaxResults(10);
        List<ExpendRecord> list=query.list();
        if(list.size()>0)
            return list;
        return null;
    }

    @Override
    public List getExpendRecordByOpenId(String openId, int startPage) {

        String hql="FROM ExpendRecord er where er.openId='"+openId+"' order by er.expendTime desc";
        String count = "SELECT COUNT(er) " + hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
//
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, FOOT_PAGE_ROW);
    }

    @Override
    public List getExpendByOpenIdOrType(String openId, String expendType) {

        String hql="FROM ExpendRecord er where er.openId=? and er.expendType=?";
        List<ExpendRecord> list= (List<ExpendRecord>) getHibernateTemplate().find(hql,openId,expendType);
        if(list.size()>0)            return list;
        return null;
    }

    @Override
    public Double getExpendMoneyByOpenId(String openId) {

        String hql="SELECT SUM(e.expendMoney) FROM ExpendRecord e where e.openId=?";

        if(getHibernateTemplate().find(hql,openId).get(0)==null)
            return 0.0;
        return (Double) getHibernateTemplate().find(hql,openId).get(0);

    }
    @Override
    public List<Object> findByUserExpendRecord(Long id , Integer pageNum) {

        /*int pageSize = 10;
        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql=" FROM ExpendRecord er where er.openId = '"+id+"'order by er.expendTime desc";
        Query query=session.createQuery(hql);
        query.setFirstResult((pageNum - 1)*10);
        query.setMaxResults(10);
        List<ExpendRecord> list=query.list();
        if (list == null || list.size() < 1){
            return null;
        }*/

        String hql="FROM ExpendRecord er where er.uid='"+id+"' order by er.expendTime desc";
        String count = "SELECT COUNT(er) " + hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
//
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, pageNum, FOOT_PAGE_ROW);

    }

}
